package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.RecommenderChangeMapper;
import com.yixun.qfbao.model.RecommenderChange;
import com.yixun.qfbao.model.dto.RecommenderChangeDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.service.RecommenderChangeService;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.UUID;


@Slf4j
@Service("recommenderChangeService")
public class RecommenderChangeServiceImpl implements RecommenderChangeService {
	
	@Autowired
	private RecommenderChangeMapper mapper;
	@Autowired
	private CustomerPremisesService customerPremisesService;

	@Transactional(readOnly = true)
	public Page<RecommenderChange> getPageModel(RecommenderChangeDto entity, int begin, int end){
		int total = mapper.count(entity);
		List<RecommenderChange> rows = mapper.list(entity,begin,end);
		return new Page<>(total, rows);
	}

	public List<RecommenderChange> selectListByDto(RecommenderChangeDto entity){
		return mapper.selectListByDto(entity);
	}
	
	public RecommenderChange selectByDto(RecommenderChangeDto entity){
		return mapper.selectByDto(entity);
	}
	
	public RecommenderChange selectById(Long id){
		if(Objects.nonNull(id)){
			return mapper.selectByPrimaryKey(id);
		}
		return null;
	}

	public RecommenderChange create(RecommenderChange entity) {
		entity.setChangeUuid(UUID.randomUUID().toString().replace("-", ""));
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		mapper.insertSelective(entity);
		return entity;
	}

	public int update(RecommenderChange entity) {
		// 修改时必须判断 id 是否为null,否则很危险
		// 如果为null,会修改全部.最终where语句为 (id is not null),没有 id = ???
		if(Objects.isNull(entity.getId())){
			return 0;
		}
		entity.setUpdateTime(Calendar.getInstance().getTime());
		Example example = new Example(RecommenderChange.class);
		example.createCriteria()
				.andIsNotNull("id")
				.andEqualTo("id", entity.getId());
		return mapper.updateByExampleSelective(entity, example);
	}

	public int saveOrUpdate(RecommenderChange entity) {
		if(Objects.nonNull(entity) && Objects.nonNull(entity.getId())){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}

	@Override
	public Boolean recommenderChange(RecommenderChange entity) {
		CustomerPremises customerPremises = customerPremisesService.selectById(entity.getCpId());
		if(Objects.isNull(customerPremises)){
			return Boolean.FALSE;
		}
		String oldRecommenderUuid = customerPremises.getRecommenderUuid();
		String newRecommenderUuid = entity.getNewRecommender();

		CustomerPremisesDto cpDto = new CustomerPremisesDto();
		cpDto.setId(customerPremises.getId());
		cpDto.setRecommenderUuid(newRecommenderUuid);
		customerPremisesService.updateStatus(cpDto);

		entity.setCpId(customerPremises.getId());
		entity.setOldRecommender(oldRecommenderUuid);
		create(entity);

		return Boolean.TRUE;
	}
}
